use actix_web::{HttpResponse, Responder};

use sysinfo::{DiskExt, System, SystemExt};

pub async fn health_check() -> impl Responder {
    HttpResponse::Ok().body("ok")
}

pub async fn cpu_check() -> impl Responder {
    // Please note that we use "new_all" to ensure that all list of
    // components, network interfaces, disks and users are already
    // filled!
    let mut sys = System::new_all();
    // First we update all information of our `System` struct.
    sys.refresh_all();
    // Number of CPUs:
    let cpu_num = format!("NB CPUs: {}", sys.cpus().len());
    HttpResponse::Ok().body(cpu_num)
}

pub async fn disk_check() -> impl Responder {
    // Please note that we use "new_all" to ensure that all list of
    // components, network interfaces, disks and users are already
    // filled!
    let mut sys = System::new_all();
    // First we update all information of our `System` struct.
    sys.refresh_all();
    let mut dist_str = String::new();
    for disk in sys.disks() {
        // 检查挂载点是否为 "/"
        if disk.mount_point().to_str().unwrap() == "/" {
            let total_space_mb = disk.total_space() as f64 / 1024.0 / 1024.0;
            let available_space_mb = disk.available_space() as f64 / 1024.0 / 1024.0;

            // println!("Disk name: {}", disk.name().to_str().unwrap());
            // println!("Total space: {:.2} MB", total_space_mb);
            // println!("Available space: {:.2} MB", available_space_mb);

            dist_str = format!(
                "{} {} {}",
                disk.name().to_str().unwrap(),
                total_space_mb,
                available_space_mb
            );
        }
    }
    HttpResponse::Ok().body(dist_str)
}
